//////////////////////////////////////////////////////////////////////////////////
//	Copyright 2011 by Yosef Grabivker, ryuho@homenetworksoft.com
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//////////////////////////////////////////////////////////////////////////////////

#include <wx/wx.h>
#include <wx/statline.h>
#include <wx/gbsizer.h>
#include "portforwardoptions.h"
#include "listselectorpanel.h"

enum cIDS
{
	cIDUseAllow = 13572,
	cIDUseBlock,
};

BEGIN_EVENT_TABLE(CPortForwardOptions, wxPanel)
	EVT_RADIOBUTTON(cIDUseAllow, CPortForwardOptions::OnUseAllow)
	EVT_RADIOBUTTON(cIDUseBlock, CPortForwardOptions::OnUseBlock) 
END_EVENT_TABLE()

CPortForwardOptions::CPortForwardOptions(wxWindow* pParent):wxPanel( pParent, wxID_ANY )
{
	int nFlags      = wxLEFT|wxUP|wxRIGHT|wxALIGN_CENTRE_VERTICAL;
	int nBorder		= 5;

	wxBoxSizer* pSizer = new wxBoxSizer(wxVERTICAL);

	//Auto Port Forwarding
	wxStaticBoxSizer* pPFBox = new wxStaticBoxSizer( new
		wxStaticBox(this, wxID_ANY,  _("Auto Port Forwarding")), wxHORIZONTAL);

	wxStaticText* pTxtPortB = new wxStaticText(this, wxID_ANY, _("Minimal port number:"));
	m_pPortForwardLowBound  = new wxTextCtrl(this, wxID_ANY, wxT(""),
		wxDefaultPosition, wxSize(50,20), 0, wxTextValidator(wxFILTER_NUMERIC, &m_strPortLowBound));

	pPFBox->Add(pTxtPortB, 0, wxALL|wxALIGN_CENTER, 5);
	pPFBox->Add(m_pPortForwardLowBound, 0, wxALL|wxALIGN_CENTER, 5);
	pSizer->Add( pPFBox, 0, wxALL|wxEXPAND, 5 );

	wxFlexGridSizer* pFlexSizer = new wxFlexGridSizer(2, 2, 5, 5);

	m_pUseAllowPolicy = new wxRadioButton(this, cIDUseAllow,  _("Use \"Listed only\" Policy"));
	m_pUseBlockPolicy = new wxRadioButton(this, cIDUseBlock,  _("Use \"All Except listed\" Policy"));
	pFlexSizer->Add(m_pUseAllowPolicy, 0, wxEXPAND|wxALIGN_LEFT|wxRIGHT, 10);
	pFlexSizer->Add(m_pUseBlockPolicy, 0, wxEXPAND|wxALIGN_RIGHT|wxLEFT, 10);
	m_pAllowPolicy = new CListSelectorPanel(this, _("Auto port forward listed programs only") );
	m_pBlockPolicy = new CListSelectorPanel(this, _("Auto port forward all, except listed") );
	pFlexSizer->Add(m_pAllowPolicy, 1, wxEXPAND|wxALIGN_LEFT|wxRIGHT, 10);
	pFlexSizer->Add(m_pBlockPolicy, 1, wxEXPAND|wxALIGN_RIGHT|wxLEFT, 10);

	pFlexSizer->AddGrowableRow(1,1);
	pFlexSizer->AddGrowableCol(0,1);
	pFlexSizer->AddGrowableCol(1,1);

	pFlexSizer->SetFlexibleDirection( wxBOTH );

	pSizer->Add( pFlexSizer, 1, wxALL|wxEXPAND, 5 );

	SetSizer(pSizer);
}

CPortForwardOptions::~CPortForwardOptions(void)
{
}

unsigned short CPortForwardOptions::GetAutoPortForwardLowPort()
{
	long l=1024;

	m_pPortForwardLowBound->GetValue().ToLong(&l);

	if (l>0xFFFF) l = 0xFFFF;

	return (unsigned short)l;
}

void CPortForwardOptions::SetAutoPortForwardLowPort(unsigned short val)
{
	m_strPortLowBound = wxString::Format(wxT("%u"),val);
	m_pPortForwardLowBound->SetValue( m_strPortLowBound );
}

void CPortForwardOptions::OnUseAllow(wxCommandEvent& event)
{
	m_pAllowPolicy->Enable( m_pUseAllowPolicy->GetValue() );
	m_pBlockPolicy->Enable( !m_pUseAllowPolicy->GetValue() );
}

void CPortForwardOptions::OnUseBlock(wxCommandEvent& event)
{
	m_pBlockPolicy->Enable( m_pUseBlockPolicy->GetValue() );
	m_pAllowPolicy->Enable( !m_pUseBlockPolicy->GetValue() );
}

void CPortForwardOptions::SetEnableAllowPFPolicy(bool bSet)
{
	m_pUseAllowPolicy->SetValue(bSet);
	OnUseAllow( wxCommandEvent() );
}

bool CPortForwardOptions::GetEnableAllowPFPolicy()
{
	return m_pUseAllowPolicy->GetValue();
}

void CPortForwardOptions::SetAllowPFPolicy(const TEXTLIST& lst )
{
	m_pAllowPolicy->SetList(lst);
}

TEXTLIST CPortForwardOptions::GetAllowPFPolicy()
{
	return m_pAllowPolicy->GetList();
}

void CPortForwardOptions::SetEnableBlockPFPolicy(bool bSet)
{
	m_pUseBlockPolicy->SetValue(bSet);
	OnUseBlock( wxCommandEvent() );
}

bool CPortForwardOptions::GetEnableBlockPFPolicy()
{
	return m_pUseBlockPolicy->GetValue();
}

void CPortForwardOptions::SetBlockPFPolicy(const TEXTLIST& lst )
{
	m_pBlockPolicy->SetList(lst);
}

TEXTLIST CPortForwardOptions::GetBlockPFPolicy()
{
	return m_pBlockPolicy->GetList();
}
